home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / v1dir.lbr / MATCHCH.PQS / matchch.pas
Pascal/Delphi Source File  |  1985-06-03  |  4KB  |  104 lines

  1. {NAME
  2.      match_char -- match a character in a string
  3.  
  4. SYNOPSIS
  5.  
  6.      match_char(match_str, ifirst, target_char, match_spec)
  7.  
  8.      str255: STRING[255].  A TYPE which must be declared before you INCLUDE
  9.                            the function.
  10.      match_str: str255.  String to search.
  11.      ifirst: INTEGER.  Index of start of search.
  12.      target_char: BYTE.  ORD of character to compare.
  13.      match_type: (next_match, last_match, next_unmatch, last_unmatch)
  14.                   next_match- find first matching character after ifirst.
  15.                   last_match- find last matching character before ifirst.
  16.                   next_unmatch- find first non-matching character after ifirst.
  17.                   last_unmatch- find last non-matching character before ifirst.
  18.      match_char: INTEGER. If the find succeeds, its index.  If not, 0.
  19.  
  20. DESCRIPTION
  21.           The find starts with ifirst.  If it meets the condition (match or
  22.      unmatch, match_char = ifirst.  If not, the index increases (next) or
  23.      decreases (last) until either the find succeeds or it runs off the string.
  24.      If it runs off the string, match_char = 0.
  25.           match_char is moderately forgiving for ifirst.  If the direction is
  26.      up (next), it starts at the beginning of the string for a nonpositive
  27.      ifirst.  If the direction is down, it starts at the end of the string for
  28.      ifirst too big.
  29.  
  30.           NOTE: These functions use MARK and RELEASE to handle the dynamic
  31.      heap.  As a result, any program which uses one of them can not use
  32.      DISPOSE.
  33.  
  34. L. Paper
  35. 11/18/84
  36.                                                                          }
  37.  
  38. TYPE
  39.   match_type = (next_match, last_match, next_unmatch, last_unmatch);
  40.  
  41. FUNCTION match_char(match_str: str255; ifirst:INTEGER; target_char: byte;
  42.                     match_spec: match_type): INTEGER;
  43.  
  44. VAR
  45.   n:INTEGER;
  46.   next, match, more: BOOLEAN;
  47.  
  48.   BEGIN
  49.  
  50.     n := LENGTH(match_str);
  51.  
  52.     IF n > 0 THEN
  53.       BEGIN
  54.         next := (match_spec = next_match) OR (match_spec = next_unmatch);
  55.         match := (match_spec = next_match) OR (match_spec = last_match);
  56.  
  57.         n := ifirst;
  58.         IF next
  59.           THEN
  60.             BEGIN
  61.               IF n < 1 THEN n := 1;
  62.               IF n > LENGTH(match_str) THEN n := 0;
  63.             END {next}
  64.           ELSE {NOT next}
  65.             BEGIN
  66.               IF n < 1 THEN n := 0;
  67.               IF n > LENGTH(match_str) THEN n := LENGTH(match_str);
  68.             END; {NOT next} {For IF next}
  69.       END; {IF n > 0}
  70.  
  71.   IF n > 0 THEN
  72.     BEGIN {Avoid dangling ELSE}
  73.       IF next
  74.         THEN
  75.           BEGIN
  76.             {Use more rather than AND to avoid evaluating
  77.              match_str[LENGTH(match_str) +1]}
  78.             IF match
  79.               THEN more := (ORD(match_str[n]) <> target_char)
  80.               ELSE more := (ORD(match_str[n]) = target_char);
  81.             WHILE more DO
  82.               BEGIN
  83.                 n := n + 1;
  84.                 more := (n <= LENGTH(match_str));
  85.                 IF more THEN
  86.                   IF match
  87.                     THEN more := (ORD(match_str[n]) <> target_char)
  88.                     ELSE more := (ORD(match_str[n]) = target_char);
  89.               END; {WHILE more}
  90.               IF n > LENGTH(match_str) THEN n := 0;
  91.             END {if next}
  92.           ELSE {last}
  93.             BEGIN
  94.               IF match
  95.                 THEN WHILE (n > 0) AND (ORD(match_str[n]) <> target_char)
  96.                        DO n := n - 1
  97.                 ELSE WHILE (n > 0) AND (ORD(match_str[n]) = target_char)
  98.                        DO n := n - 1;
  99.               IF n <= 0 THEN n := 0;
  100.             END; {last} {For IF next}
  101.     END; {IF n > 0}
  102.   match_char := n;
  103. END; {match_char}
  104.